﻿namespace YunQue.WebApi.Common.JWT
{
    /// <summary>
    /// jwt帮助类
    /// </summary>
    public class JwtHelper
    {
        /// <summary>
        /// 生成token
        /// </summary>
        /// <param name="user"></param>
        /// <param name="token"></param>
        /// <param name="sessionId"></param>
        /// <returns></returns>
        public static string CreateToken(UserInfo user,string token, string sessionId)
        {
            var claims = new[]
            {
                new Claim(ClaimTypes.Name,user.Name),
                new Claim(ClaimTypes.NameIdentifier,user.Id),
                new Claim(AppSettingManager.JWT_TENANT,token),
                new Claim(AppSettingManager.JWT_SESSIONID,sessionId)
            };
            var secretkey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(AppSettingManager.JWT_SECRET));
            var algorithm = SecurityAlgorithms.HmacSha256;
            var signingCredentials = new SigningCredentials(secretkey, algorithm);
            var jwtToken = new JwtSecurityToken(
                AppSettingManager.JWT_ISSUER,
                AppSettingManager.JWT_AUD,
                claims,
                DateTime.Now,
                DateTime.Now.AddDays(7),
                signingCredentials
                );
            return new JwtSecurityTokenHandler().WriteToken(jwtToken);
        }
    }
}
